package com.zkgl.rent.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zkgl.common.annotation.Log;
import com.zkgl.common.core.controller.BaseController;
import com.zkgl.common.core.domain.AjaxResult;
import com.zkgl.common.core.domain.entity.SysUser;
import com.zkgl.common.enums.BusinessType;
import com.zkgl.rent.domain.Property;
import com.zkgl.rent.service.IPropertyService;
import com.zkgl.common.utils.poi.ExcelUtil;
import com.zkgl.common.core.page.TableDataInfo;

/**
 * 房源信息Controller
 * 
 * @author macro
 * @date 2025-09-25
 */
@RestController
@RequestMapping("/rent/property")
public class PropertyController extends BaseController {
    @Autowired
    private IPropertyService propertyService;

    /**
     * 查询房源信息列表
     */
    @PreAuthorize("@ss.hasPermi('rent:property:list')")
    @GetMapping("/list")
    public TableDataInfo list(Property property) {
        startPage();
        List<Property> list = propertyService.selectPropertyList(property);
        return getDataTable(list);
    }
    
    /**
     * 查询房源信息列表
     */
    @PreAuthorize("@ss.hasPermi('rent:property:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(Property property) {
        List<Property> list = propertyService.selectPropertyList(property);
        return getDataTable(list);
    }

    /**
     * 导出房源信息列表
     */
    @PreAuthorize("@ss.hasPermi('rent:property:export')")
    @Log(title = "房源信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Property property) {
        List<Property> list = propertyService.selectPropertyList(property);
        ExcelUtil<Property> util = new ExcelUtil<Property>(Property.class);
        util.exportExcel(response, list, "房源信息数据");
    }

    /**
     * 获取房源信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('rent:property:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Property property = propertyService.selectPropertyById(id);
        // 判断是否为本人房源
        if (!SysUser.isAdmin(getUserId())) {
            if (!getUserId().equals(property.getLandlordId())) {
                return error("无权查看此房源信息，如有疑问请联系管理员");
            }
        }
        return success(property);
    }

    /**
     * 新增房源信息
     */
    @PreAuthorize("@ss.hasPermi('rent:property:add')")
    @Log(title = "房源信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Property property) {
        property.setLandlordId(getUserId());
        return toAjax(propertyService.insertProperty(property));
    }

    /**
     * 修改房源信息
     */
    @PreAuthorize("@ss.hasPermi('rent:property:edit')")
    @Log(title = "房源信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Property property) {
        Property propertyDb = propertyService.selectPropertyById(property.getId());
        // 判断是否为本人房源
        if (!SysUser.isAdmin(getUserId())) {
            if (!getUserId().equals(propertyDb.getLandlordId())) {
                return error("无权修改此房源信息，如有疑问请联系管理员");
            }
        }
        return toAjax(propertyService.updateProperty(property));
    }

    /**
     * 删除房源信息
     */
    @PreAuthorize("@ss.hasPermi('rent:property:remove')")
    @Log(title = "房源信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(propertyService.deletePropertyByIds(ids));
    }
}
